using UnityEngine;
using UnityEditor;
using System.Collections;


[CustomPropertyDrawer(typeof(PropSet))]
public class PropSetDrawer : PropertyDrawer
{
    float _height = 18;
    bool _showing = true; //showing


    /// <summary>
    /// 获取控件高度
    /// </summary>
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        SerializedProperty props = property.FindPropertyRelative("_props");

        if (!_showing) return _height;

        return _height * (props.arraySize);
    }


    /// <summary>
    /// 重绘属性对象面板
    /// </summary>
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EditorGUI.BeginProperty(position, label, property);
        Rect titleRect = new Rect(position.x, position.y, position.width, _height);
        if (!(_showing = EditorGUI.Foldout(titleRect, _showing, "属性信息(" + label.text + ")"))) return;

        SerializedProperty props = property.FindPropertyRelative("_props");

        for (int i = (int)PropType.None, iMax = (int)PropType.MAX - 1; i < iMax; ++i)
        {
            int propIndex = i + 1;
            string propDesc = ((PropType)propIndex).ToString();
            Rect propRect = new Rect(position.x, position.y + propIndex * _height, position.width, _height);
            EditorGUI.PropertyField(propRect, props.GetArrayElementAtIndex(propIndex), new GUIContent(propDesc, propDesc));
        }

        EditorGUI.EndProperty();
    }
}
